package BallGame;
//抽象窗口工具箱 重量级
import java.awt.*;
//图形可视包 轻量级
import javax.swing.*;

public class BallGame extends JFrame{

    Image ball=Toolkit.getDefaultToolkit().getImage("image/ball.png");
    Image desk=Toolkit.getDefaultToolkit().getImage("image/desk.png");

    double x=100;
    double y=100;
    boolean right=true;//如果是true方向向右反之向左

    public void paint(Graphics g){
        System.out.println("窗口被调用了!");
        g.drawImage(desk,0,30,null);
        g.drawImage(ball,(int)x,(int)y,null);
        if(right){
            x=x+10;
        }else {
            x=x-10;
        }
        if(x>856-70){
            right=false;
        }
        if(x<50){
            right=true;
        }
    }

    //窗口加载
    public void launchFrame(){
        setSize(856,470);
        setLocation(50,50);
        setVisible(true);

        //重画窗口
        while (true){
            repaint();
            try{
                Thread.sleep(40);
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }
    //main方法是程序执行的入口
    public static void main(String[] args) {
        BallGame game=new BallGame();
        game.launchFrame();

    }
}
